package com.ocom.payhandle.fun.impl;

import com.ocom.common.entity.card.EntityXfRecord;
import com.ocom.common.entity.card.EntityXfRecordProcessExxx;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payhandle.fun.ShoppingCartPayLimitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Stream;


@Slf4j
@Service
public class ShoppingCartPayLimitServiceImpl implements ShoppingCartPayLimitService {
    @Override
    public Result xfUserparamDiscount(EntityXfRecord entityXfRecord, List<EntityXfRecordProcessExxx> processExxxList) {

        Stream<EntityXfRecordProcessExxx> exxxes2 = processExxxList.stream();


//        QueryWrapper<EntityXfRecord> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("count(id) as times","sum(discount) as amount");
//        queryWrapper.eq("p_id",entityXfRecord.getPId());
//        queryWrapper.eq("com_id",entityXfRecord.getComId());
//        queryWrapper.eq("cons_state",1);
//        queryWrapper.eq("pay_state",1);
//        queryWrapper.eq("if_refund",0);
//        queryWrapper.gt("discount",0);
//        queryWrapper.eq("cons_type",entityXfRecord.getConsType());

        exxxes2 = exxxes2.filter(o -> o.getDiscount() > 0l);

        if(entityXfRecord.getReserved1()!=null && entityXfRecord.getReserved1().equals("selectMerchTrue")){
//            queryWrapper.eq("merch_id",entityXfRecord.getMerchId());
            exxxes2 = exxxes2.filter(o -> o.getMerchId().equals(entityXfRecord.getMerchId()));
        }

        String strDate = DateUtils.getStrDate(new Date(), DateUtils.YYYY_MM_DD_HH_MM_SS);
        if (entityXfRecord.getConsTime() != null) {
            strDate = DateUtils.getStrDate(entityXfRecord.getConsTime(), DateUtils.YYYY_MM_DD_HH_MM_SS);
        }

        String endMonth = strDate.substring(0,7).replace("-","");
        String startMonth = DateUtils.getLastMonth(endMonth, 1);

        List<String> dates = new ArrayList<>();
        dates.add(endMonth);
        dates.add(startMonth);
        Integer mealNo = Integer.valueOf(entityXfRecord.getMealNo());
        if (entityXfRecord.getMealNo().equals("500")){
            //餐段按天
            strDate = strDate.substring(0,10);
//            queryWrapper.eq("meal_date", strDate);
            String finalStrDate1 = strDate;
            exxxes2 = exxxes2.filter(o -> DateUtil.getDate(o.getMealDate()).equals(finalStrDate1));
        }else if (entityXfRecord.getMealNo().equals("1000")){
            //按月查询
            strDate = strDate.substring(0, 7);
            String endDate = DateUtils.getLastDayOfMonth(Integer.valueOf(strDate.substring(5)));
//            queryWrapper.between("meal_date",strDate + "-01", endDate);

            strDate = strDate + "-01";
            String finalStrDate1 = strDate;
            exxxes2 = exxxes2.filter(o -> o.getMealDate().compareTo(DateUtils.strToDate2(finalStrDate1)) >= 0);
            String finalStrDate2 = endDate;
            exxxes2 = exxxes2.filter(o -> o.getMealDate().compareTo(DateUtils.strToDate2(finalStrDate2)) <= 0);

        }else if (mealNo>900 && mealNo<1000){
            //按月查询
            strDate = strDate.substring(0, 7);
            String endDate = DateUtils.getLastDayOfMonth(Integer.valueOf(strDate.substring(5)));
//            queryWrapper.between("meal_date",strDate + "-01", endDate);

            strDate = strDate + "-01";
            String finalStrDate1 = strDate;
            exxxes2 = exxxes2.filter(o -> o.getMealDate().compareTo(DateUtils.strToDate2(finalStrDate1)) >= 0);
            String finalStrDate2 = endDate;
            exxxes2 = exxxes2.filter(o -> o.getMealDate().compareTo(DateUtils.strToDate2(finalStrDate2)) <= 0);
            exxxes2 = exxxes2.filter(o -> o.getMealNo().equals(String.valueOf(mealNo-900)));
        }else {
//            queryWrapper.eq("meal_no",entityXfRecord.getMealNo());
            exxxes2 = exxxes2.filter(o -> o.getMealNo().equals(entityXfRecord.getMealNo()));
            strDate = strDate.substring(0,10);
//            queryWrapper.eq("meal_date", strDate);
            String finalStrDate1 = strDate;
            exxxes2 = exxxes2.filter(o -> DateUtil.getDate(o.getMealDate()).equals(finalStrDate1));
        }
//        EntityXfRecord entityXfRecord1 = new EntityXfRecord();
//        entityXfRecord1.setTimes(0);
//        entityXfRecord1.setAmount(0l);
//        for (String month : dates) {
//            QueryWrapper<EntityXfRecord> queryWrapper2 = queryWrapper.clone();
//            queryWrapper2.eq("month_value", month);
//            EntityXfRecord entityXfRecords3 = xfRecordMapper.selectOne(queryWrapper2);
//            entityXfRecord1.setTimes(entityXfRecord1.getTimes() + entityXfRecords3.getTimes());
//            if(entityXfRecords3.getAmount()!=null) entityXfRecord1.setAmount(entityXfRecord1.getAmount() + entityXfRecords3.getAmount());
//        }

        LongSummaryStatistics statistics = exxxes2.mapToLong(EntityXfRecordProcessExxx::getDiscount).summaryStatistics();

        Map<String, Object> map = new HashMap<>();
        map.put("times", statistics.getCount());
        map.put("amount", statistics.getSum());

        return ResultUtil.success(map);

    }
}
